/*!
 * Copyright (C) 2020 - All Rights Reserved by
 * @author : ZhaoYanbo
 * @email  : zyb920@hotmail.com
 * @created: 2020-10-31
 * @version: 1.0.0.0
 *
 */

#include "le_ruler.h"
#include <algorithm>
#include <QPainter>
#include <QtDebug>

LE_Ruler::LE_Ruler()
{
    m_zoom[0] = 1.00f;
    m_zoom[1] = 0.50f;
    m_zoom[2] = 0.34f;
    m_zoom[3] = 0.25f;
    m_zoom[4] = 0.20f;

#if 0
    m_zoomCount = sizeof (m_zoom) / sizeof (float);
#else
    m_zoomCount = 5;
#endif
    m_zoomIndex = 0;

    m_bMove = false;
    m_startValue = 0.00f;
    setColor("#545556", "#C0C0C0");
}

LE_Ruler::~LE_Ruler()
{
    reset();
}

bool LE_Ruler::zoomIn()
{
    if( canZoomIn() )
    {
        ++m_zoomIndex;
        return true;
    }
    return false;
}

bool LE_Ruler::canZoomIn() const
{
    //如果是最后一个, 不能再放大
    return ( (m_zoomIndex < m_zoomCount-1) && (m_zoomIndex >= 0) );
}

bool LE_Ruler::zoomOut()
{
    if( canZoomOut() )
    {
        --m_zoomIndex;
        return true;
    }
    return false;
}

bool LE_Ruler::canZoomOut() const
{
    //如果是第一个,不能再缩小
    return ( (m_zoomIndex < m_zoomCount) && (m_zoomIndex > 0) );
}

void LE_Ruler::setColor(const QString &lineColor, const QString &textColor)
{
    m_lineColor = lineColor;
    m_textColor = textColor;
}

void LE_Ruler::reset()
{
    m_zoomIndex = 0;
}

float LE_Ruler::pos2value(int x)
{
    // 120 = 间距(12px) * 10格
    float value = m_startValue +  (m_zoom[m_zoomIndex] * x / 120) ;
    return value;
}

int LE_Ruler::value2pos(float value)
{
    int x = (value - m_startValue) * 120 / (m_zoom[m_zoomIndex]);
    return x;
}

void LE_Ruler::setCurStartValue(float v)
{
    m_startValue = v;
}

void LE_Ruler::doMoveStart()
{
    if(!m_bMove)
    {
        m_bMove = true;
        m_moveValue = m_startValue;
    }
}

void LE_Ruler::doMove(int x)
{
    if(m_bMove)
    {
        float v = m_moveValue +  (m_zoom[m_zoomIndex] / 120) * x;
        m_startValue = v;
        if(m_startValue < 0.001f)
            m_startValue = 0.00f;
    }
}

bool LE_Ruler::doMove()
{
    float v = m_startValue - m_moveValue;
    if(std::abs(v) >= 0.001f)
    {
        m_startValue = m_moveValue;
        return true;
    }
    return false;
}

void LE_Ruler::doMoveEnd()
{
    m_bMove = false;
}

QPixmap LE_Ruler::update(int width, int height)
{
    QPixmap pix(width, height);
    pix.fill(Qt::transparent);

    QPainter p;
    p.begin(&pix);

    p.setRenderHint(QPainter::Antialiasing);
    p.setRenderHint(QPainter::TextAntialiasing);

    QPen pen;
    pen.setWidth(2);
    pen.setColor(m_lineColor);
    p.setPen(pen);

    int rectX = 0;    //当前绘制坐标
    int rectIndex = 0;//当前索引
    const int space = 12;//间距
    while(1)
    {
        rectX = space * rectIndex;
        if(rectX > width)
            break;
        if(rectIndex % 10 == 0)
        {
            p.drawLine(rectX, 0, rectX, height);
        }
        else
        {
            p.drawLine(rectX, height-4, rectX, height);
        }
        ++rectIndex;
    }
    p.drawLine(0, height, width, height);

    pen.setColor(m_textColor);
    p.setPen(pen);

    QFont font = p.font();
    font.setPixelSize(16);
    p.setFont(font);

    float spaceValue = m_zoom[m_zoomIndex];
    float time = m_startValue;
    rectIndex = 0;
    while(1)
    {
        rectX = space * rectIndex;
        if(rectX > width)
            break;

        if(rectIndex % 10 == 0)
        {
            p.drawText(rectX+2, height-6, QString::number(time, 'f', 2));
            time += spaceValue;
        }
        ++rectIndex;
    }

    p.end();

    return pix;
}
